package cn.bdqn.controller.user;

import cn.bdqn.pojo.User.SearchRecords;
import cn.bdqn.service.user.SearchRecordsService;
import cn.bdqn.utils.Json;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/searchRecords")
public class SearchRecordsController {
    @Autowired
    SearchRecordsService service;

    Json json=new Json();

    /**
     * 查询所有搜题记录
     * @param curr
     * @param size
     * @param userId 必传
     * @return
     */
    @GetMapping("/selectAllRecords")
    public Json selectAllRecords(Integer curr,Integer size,Integer userId){
        IPage page = service.selectAll(curr, size,userId);
        if(null!=page){
            json.setCode(200);
            json.setData(page.getRecords());
            json.setTotal((int)page.getTotal());
        }else{
            json.setCode(400);
        }
        return json;
    }

    @DeleteMapping("/deleteSearchRecords")
    public Json dropSearchRecords(Integer userId){
        Integer drop = service.drop(userId);
        if(drop>=0){
            json.setCode(200);
        }else{
            json.setCode(400);
        }
        return json;
    }

    @PostMapping("/insertRecords")
    public Json insertRecords(@RequestBody SearchRecords searchRecords){
        Integer insert = service.insert(searchRecords);
        if(insert>=0){
            json.setCode(200);
        }else{
            json.setCode(400);
        }
        return json;
    }
}
